﻿using System;
using System.Collections.Generic;
using System.Text;
using OABackground.Entities;
using OABackground.Infrastructure;

namespace OABackground.Services
{
    public class NeedConvention
    {
        public DateTime startTime;
        public DateTime endTime;
        public string place;
        public string state;
    }
    public class ConventionApplyService
    {
        public int AddConventionApplyList(string contact, string startime, string endtime, string conventionplace)
        {
            int CA;
            if (contact == null)
            {
                CA = -1;
            }
            else if (startime == null || endtime == null)
                CA = 0;
            else if (conventionplace == null)
                CA = 1;
            else
            {
                var ConventionApply = new Conventionapply
                {
                    Contact = contact,
                    StartTime = Convert.ToDateTime(startime),
                    EndTime = Convert.ToDateTime(endtime),
                    ConventionPlace = conventionplace,
                    ConventionState = 0
                };
                CA = AddConventionApply(ConventionApply);
            }
            return CA;
        }
        public int AddConventionApply(Conventionapply model)//添加新表项
        {
            int CA;
            if (JudgeTime(model.StartTime, model.EndTime) == false)
                CA = 2;
            else
            {
                using (var dbConnect = new oaContext())
                {
                    var repo = new ConventionApplyRepository();
                    repo.Add(model);
                    CA = 3;
                }
            }
            return (CA);
        }
        public List<NeedConvention> QueryConventionByTid(string Id)
        {
            var meetting = new ConventionApplyRepository();
            var convention = meetting.QueryByTeacher(Id);
            List<NeedConvention> ConventionList = new List<NeedConvention>();
            for (int i = 0; i < convention.Count; i++)
            {
                NeedConvention needConvention = new NeedConvention
                {
                    startTime = convention[i].StartTime,
                    endTime = convention[i].EndTime,
                    place = convention[i].ConventionPlace
                };
                switch (convention[i].ConventionState)
                {
                    case 0:
                        needConvention.state = "正在审核";
                        break;
                    case 1:
                        needConvention.state = "通过申请";
                        break;
                    case 2:
                        needConvention.state = "已被拒绝";
                        break;
                    default:
                        break;
                }
                ConventionList.Add(needConvention);
            }
            return ConventionList;
        }
        public bool JudgeTime(DateTime starttime, DateTime endtime)
        {
            var CheckConvention = new ConventionApplyRepository();
            bool flag1 = CheckConvention.QueryByStarttime(starttime);
            bool flag2 = CheckConvention.QueryByEndtime(endtime);
            if (flag1 == true && flag2 == true)
                return true;
            else
                return false;

        }
    }
}
